import React from 'react';
import {Spin, Card, Button, Row, Col, Form} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {FORM_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import {hasPermissions} from './utils';
import {injectIntl} from './intl';
import styles from './style.css';
import TextArea from 'Shared/components/TextArea';
import Return from '../common/Return';
import {orderValidator} from './validator';

const FormItem = Form.Item;
class OrderPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        sapOrderCode: []
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    onChange = value => {
        this.setState({
            sapOrderCode: value,
            hasOperat: true
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        const {sapOrderCode} = this.state;
        const isValid = orderValidator(sapOrderCode);
        if(!isValid) {
            this.setState({
                validate: true
            });
            return;
        }
        this.setState({loading: true});
        this.props.onSubmit(sapOrderCode).then(res => {
            if(res.success)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {sapOrderCode, loading} = this.state;
        const validateSapOrderCode = sapOrderCode.length < 1 && this.state.validate ? 'error' : null;
        const submitable = !hasPermissions(PERMISSION.order, this.props.permissions);
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'orderPanel.column.common',
                        defaultMessage: '订单基本信息'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'orderPanel.operation.sapOrderCode',
                                        defaultMessage: 'SAP销售订单号'
                                    })} {...FORM_OPTIONS.item}
                                    validateStatus={validateSapOrderCode}
                                    required>
                                        <TextArea
                                            name="sapOrderCode"
                                            placeholder={this.props.intl.formatMessage({
                                                id: 'queryPanel.form.code.sapOrderCode',
                                                defaultMessage: '最多输入1000个SAP销售订单号，以回车隔开'
                                            })}
                                            value={this.state.sapOrderCode}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card className={styles.optionBtn}>
                        <Button
                            type="primary"
                            onClick={this.onSubmit}
                            disabled={!submitable}
                            loading={loading}>
                            {formatMessage({
                                id: 'orderPanel.operation.onSave',
                                defaultMessage: '保存'
                            })}
                        </Button>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

OrderPanel.propTypes = {
    permissions: PropTypes.array.isRequired,
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object
};

import {connect} from 'react-redux';
import {
    tableSearch,
    onSubmit
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSubmit: data => dispatch(onSubmit(data)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(OrderPanel));
